package com.efuture.chaptersix

/* @Description: Kotlin语言 reifid关键字学习
 * @Auther: zhaoshanshan
 * @Date: 2022/03/08/5:34 下午
 */
data class Demo105Test1(val name:String,val age:Int)
data class Demo105Test2(val name:String,val age:Int)
data class Demo105Test3(val name:String,val age:Int)
data class Demo105Test4(val name:String,val age:Int)


class Demo105{

    inline  fun<reified T> randomOrDefault(lambdaAction:()->T):T{
        val objList:List<Any?> = listOf(Demo105Test1("zhaoshanshan",30),Demo105Test2("liuyifan",36),Demo105Test3("tongshuai",31))
        val randomObj = objList.shuffled().first()
        println("随机生成的对象是$randomObj")
        //reified 关键字使范型 it is T 具有判断能力
        return randomObj.takeIf { it is T} as T? ?:lambdaAction()

    }

}

fun main() {

    val p = Demo105()
    val r = p.randomOrDefault {
        Demo105Test4("备用",100)
    }
    println("r$r")

}